package cn.xzhang.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.xzhang.boot.common.exception.ServiceException;
import cn.xzhang.boot.common.pojo.PageResult;
import cn.xzhang.boot.mapper.WallpaperCategoriesMapper;
import cn.xzhang.boot.model.dto.wallpaperCategories.WallpaperCategoriesAddReqDTO;
import cn.xzhang.boot.model.dto.wallpaperCategories.WallpaperCategoriesPageReqDTO;
import cn.xzhang.boot.model.dto.wallpaperCategories.WallpaperCategoriesStatusReqDTO;
import cn.xzhang.boot.model.dto.wallpaperCategories.WallpaperCategoriesUpdateReqDTO;
import cn.xzhang.boot.model.entity.WallpaperCategories;
import cn.xzhang.boot.model.enums.CommonStatusEnum;
import cn.xzhang.boot.model.vo.wallpaperCategories.WallpaperCategoriesSimpleVo;
import cn.xzhang.boot.model.vo.wallpaperCategories.WallpaperCategoriesVo;
import cn.xzhang.boot.service.WallpaperCategoriesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.xzhang.boot.common.exception.enums.GlobalErrorCodeConstants.*;
import static cn.xzhang.boot.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 针对表【wallpaperCategories(壁纸分类表)】的数据库操作Service实现
 *
 * @author <a href="https://github.com/XiaoZhangCode">XiaoZhangCode</a>
 */
@Service
public class WallpaperCategoriesServiceImpl extends ServiceImpl<WallpaperCategoriesMapper, WallpaperCategories> implements WallpaperCategoriesService {

    @Resource
    private WallpaperCategoriesMapper wallpaperCategoriesMapper;

    /**
     * 添加新壁纸分类
     *
     * @param wallpaperCategoriesReqDTO 壁纸分类信息请求DTO
     * @return 添加成功返回壁纸分类id
     */
    @Override
    public long addWallpaperCategories(WallpaperCategoriesAddReqDTO wallpaperCategoriesReqDTO) {
        WallpaperCategories wallpaperCategories = new WallpaperCategories();
        BeanUtil.copyProperties(wallpaperCategoriesReqDTO, wallpaperCategories);
        if (!this.save(wallpaperCategories)) {
            throw exception(ADD_FAIL);
        }
        return wallpaperCategories.getId();
    }

    /**
     * 更新壁纸分类信息
     *
     * @param wallpaperCategoriesReqDTO 壁纸分类信息更新请求DTO
     * @return 更新成功返回true
     */
    @Override
    public boolean updateWallpaperCategories(WallpaperCategoriesUpdateReqDTO wallpaperCategoriesReqDTO) {
        if (wallpaperCategoriesReqDTO.getId() == null) {
            throw exception(BAD_REQUEST);
        }
        WallpaperCategories wallpaperCategories = new WallpaperCategories();
        BeanUtil.copyProperties(wallpaperCategoriesReqDTO, wallpaperCategories);
        boolean b = this.updateById(wallpaperCategories);
        if (!b) {
            throw exception(UPDATE_FAIL);
        }
        return true;
    }

    /**
     * 删除壁纸分类
     *
     * @param id 壁纸分类id
     * @return 删除成功返回true
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public boolean deleteWallpaperCategories(Long id) {
        if (id == null) {
            throw exception(BAD_REQUEST);
        }
        boolean b = this.removeById(id);
        if (!b) {
            throw exception(DELETE_FAIL);
        }
        return true;
    }

    /**
     * 将壁纸分类对象转换为壁纸分类VO对象
     *
     * @param wallpaperCategories 壁纸分类对象
     * @return 返回壁纸分类VO对象
     */
    @Override
    public WallpaperCategoriesSimpleVo getSimpleWallpaperCategoriesVO(WallpaperCategories wallpaperCategories) {
        if (wallpaperCategories == null) {
            return null;
        }
        WallpaperCategoriesSimpleVo wallpaperCategoriesSimpleVo = new WallpaperCategoriesSimpleVo();
        BeanUtil.copyProperties(wallpaperCategories, wallpaperCategoriesSimpleVo);
        return wallpaperCategoriesSimpleVo;
    }

    /**
     * 获取壁纸分类分页信息
     *
     * @param wallpaperCategoriesPageReqDTO 壁纸分类分页请求DTO
     * @return 返回壁纸分类分页结果
     */
    @Override
    public PageResult<WallpaperCategoriesVo> getWallpaperCategoriesPage(WallpaperCategoriesPageReqDTO wallpaperCategoriesPageReqDTO) {
        PageResult<WallpaperCategories> pageResult = wallpaperCategoriesMapper.selectPage(wallpaperCategoriesPageReqDTO);
        if (pageResult.getList() == null) {
            return PageResult.empty();
        }
        List<WallpaperCategoriesVo> wallpaperCategoriesVos = pageResult.getList().stream().map(wallpaperCategories -> {
            WallpaperCategoriesVo wallpaperCategoriesVo = new WallpaperCategoriesVo();
            BeanUtil.copyProperties(wallpaperCategories, wallpaperCategoriesVo);
            return wallpaperCategoriesVo;
        }).collect(Collectors.toList());
        return new PageResult<>(wallpaperCategoriesVos, pageResult.getTotal());
    }

    @Override
    public WallpaperCategoriesVo getWallpaperCategoriesVO(WallpaperCategories wallpaperCategories) {
        if (wallpaperCategories == null) {
            return null;
        }
        WallpaperCategoriesVo wallpaperCategoriesVo = new WallpaperCategoriesVo();
        BeanUtil.copyProperties(wallpaperCategories, wallpaperCategoriesVo);
        return wallpaperCategoriesVo;
    }

    @Override
    public Boolean updateWallpaperCategoriesEnabled(WallpaperCategoriesStatusReqDTO wallpaperCategoriesEnabledReqDTO) {
        CommonStatusEnum statusEnum = CommonStatusEnum.getEnumByValue(wallpaperCategoriesEnabledReqDTO.getStatus());
        if(statusEnum==null){
            throw exception(BAD_REQUEST_PARAMS_ERROR,"状态参数错误");
        }
        WallpaperCategories categories = new WallpaperCategories();
        categories.setEnabled(wallpaperCategoriesEnabledReqDTO.getStatus().byteValue());
        categories.setId(wallpaperCategoriesEnabledReqDTO.getId());
        return this.updateById(categories);
    }

    @Override
    public Boolean updateWallpaperCategoriesSelected(WallpaperCategoriesStatusReqDTO wallpaperCategoriesSelectedReqDTO) {
        CommonStatusEnum statusEnum = CommonStatusEnum.getEnumByValue(wallpaperCategoriesSelectedReqDTO.getStatus());
        if(statusEnum==null){
            throw exception(BAD_REQUEST_PARAMS_ERROR,"状态参数错误");
        }
        WallpaperCategories categories = new WallpaperCategories();
        categories.setSelected(wallpaperCategoriesSelectedReqDTO.getStatus().byteValue());
        categories.setId(wallpaperCategoriesSelectedReqDTO.getId());
        return this.updateById(categories);
    }

}



